package analyzemytime.reports;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import analyzemytime.datamodel.Employee;
import analyzemytime.datamodel.Project;
import analyzemytime.datamodel.Report;
import analyzemytime.utils.PrintReport;

public class MonthlyEmployeeReport {
	HashMap<Project,Double> monthlyEmployeeReportMap;
	private HashMap<Employee, ArrayList<Report>> reportsByEmployee;
	private HashMap<Employee,HashMap<Project,Double>> monthlyEmployeeOverallReportMap = new HashMap<Employee, HashMap<Project,Double>>();
	
	public HashMap<Employee, HashMap<Project, Double>> getMonthlyEmployeeOverallReportMap() {
		return monthlyEmployeeOverallReportMap;
	}

	public void setMonthlyEmployeeOverallReportMap(
			HashMap<Employee, HashMap<Project, Double>> monthlyEmployeeOverallReportMap) {
		this.monthlyEmployeeOverallReportMap = monthlyEmployeeOverallReportMap;
	}
	
	public MonthlyEmployeeReport(HashMap<Employee, ArrayList<Report>> reportsAggregatedByEmployee) {
		this.monthlyEmployeeReportMap=new HashMap<Project,Double>();
		this.reportsByEmployee=reportsAggregatedByEmployee;
	}
	
	public void generateMonthlyEmployeeReport(String name,String surname,String month,String year)
	{
//		System.out.println("name "+name);
//		System.out.println("surname "+surname);
		Iterator<Employee> monthlyReportByEmployeeIterator=reportsByEmployee.keySet().iterator();
		while(monthlyReportByEmployeeIterator.hasNext())
		{
			Employee key = monthlyReportByEmployeeIterator.next();
			ArrayList<Report> reportList=reportsByEmployee.get(key);
			for(Report report: reportList)//Employee employee,Project project,String date, double reportedTime
			{
				//System.out.println("rep");
//				System.out.println("report name "+report.getEmployee().getFirstName());
//				System.out.println("report surname "+report.getEmployee().getLastName());
				if(report.getEmployee().getFirstName().equals(name) && report.getEmployee().getLastName().equals(surname))//if we have the employee we're looking for	
				{
					if(report.getDate().getYear() == Integer.parseInt(year))//if we have the year we're looking for
					{
						if(report.getDate().getMonthOfYear() == Integer.parseInt(month))//if we have the month we're looking for
						{
							Project mapKey = report.getProject();
							Double mapValue = report.getTimeReported();
							if(!monthlyEmployeeReportMap.containsKey(mapKey))
							{
								monthlyEmployeeReportMap.put(mapKey,mapValue);
							}
							else
							{
								monthlyEmployeeReportMap.put(mapKey,monthlyEmployeeReportMap.get(mapKey)+mapValue);//previous value + current value
							}
						}
					}
				}
			}
			monthlyEmployeeOverallReportMap.put(key,monthlyEmployeeReportMap);
		}
		
	}
	
	public void generateMonthlyEmployeeOverallReport(String month,String year)
	{
		Iterator<Employee> monthlyReportOverallIterator=reportsByEmployee.keySet().iterator();
		while(monthlyReportOverallIterator.hasNext())
		{
			Employee key = monthlyReportOverallIterator.next();
			HashMap<Project,Double> tempMap = new HashMap<Project, Double>();
			ArrayList<Report> reportList=reportsByEmployee.get(key);
			for(Report report: reportList)//Employee employee,Project project,String date, double reportedTime
			{
				if(report.getDate().getYear() == Integer.parseInt(year) && report.getDate().getMonthOfYear() == Integer.parseInt(month))//if we have the year we're looking for
				{
						Project mapKey = report.getProject();
						Double mapValue = report.getTimeReported();
						if(!tempMap.containsKey(mapKey))
						{
							tempMap.put(mapKey,mapValue);
						}
						else
						{
							tempMap.put(mapKey,tempMap.get(mapKey)+mapValue);//previous value + current value
						}
				}
				
			}
			System.out.println("");
			monthlyEmployeeOverallReportMap.put(key, tempMap);
		}
	}
	
	public HashMap<Project, Double> getMonthlyEmployeeReportMap() {
		return monthlyEmployeeReportMap;
	}

	public void setMonthlyEmployeeReportMap(
			HashMap<Project, Double> monthlyEmployeeReportMap) {
		this.monthlyEmployeeReportMap = monthlyEmployeeReportMap;
	}

	public void printMonthlyEmployeeReportToConsole(String name,String surname,String month,String year)
	{
		System.out.println("Employee: "+name+" "+surname+" Month: "+month+" Year: "+year);
		System.out.println("Project | hours");
		Iterator<Project> monthlyEmployeeReportIterator=monthlyEmployeeReportMap.keySet().iterator();//this iterator needs to be created each time we want to print something out in the console!
		while(monthlyEmployeeReportIterator.hasNext())
		{
			Project project=monthlyEmployeeReportIterator.next();
			System.out.println(project.toString()+"    "+monthlyEmployeeReportMap.get(project));
		}	
	}
	
	//TODO
	public void printMonthlyEmployeeReportToFile(String name,String surname,String month,String year)
	{
		String content = "";
		
		content += "Employee,"+name+","+surname+",Month,"+month+",Year,"+year+"\n\r";
		content += "Project,hours\n\r";
		Iterator<Project> monthlyEmployeeReportIterator=monthlyEmployeeReportMap.keySet().iterator();//this iterator needs to be created each time we want to print something out in the console!
		while(monthlyEmployeeReportIterator.hasNext())
		{
			Project project=monthlyEmployeeReportIterator.next();
			content += project.toString()+","+monthlyEmployeeReportMap.get(project)+"\n\r";
		}	
		
		PrintReport.PrintToFile(content, year+"_"+month+"_"+name+"_"+surname);
	}
	
	//TODO
	public void drawMonthlyEmployeeReportGraph()
	{
		
	}

}
